package com.power.sifa.approve.dao.impl;

import com.alibaba.fastjson.JSON;
import com.plf.core.base.model.Ha_calendar;
import com.plf.core.common.dao.OffersetTransformers;
import com.power.sifa.approve.dao.BusinessApproveDao;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.query.internal.NativeQueryImpl;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Repository
public class BusinessApproveDaoImpl implements BusinessApproveDao {

    @Resource
    private EntityManager entityManager;

    @Override
    public Date getTargetDay(Date date, int days) {
        Date targetDay = null;
        if (days == 0) {
            return date;
        }
        StringBuffer sql = new StringBuffer();
        if (days > 0) {
            sql.append("select gregorian from (select gregorian, row_number() over(order by id asc) rn from ha_calendar where gregorian>? and iswork=3200) x where rn=?");
        } else {
            sql.append("select gregorian from (select gregorian, row_number() over(order by id desc) rn from ha_calendar where gregorian<? and iswork=3200) x where rn=?");
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.setParameter(1, date);
        query.setParameter(2, Math.abs(days));
        List<Object> re = query.getResultList();
        if (re != null && re.size() > 0) {
            targetDay = (Date) re.get(0);
        }
        return targetDay;
    }

    @Override
    public String expiryDate(Date date, int days) {
        StringBuffer sql = new StringBuffer();
        sql.append("select to_char(gregorian, 'yyyy-MM-dd') as gregorian, iswork FROM ha_calendar where gregorian > ? ORDER BY gregorian OFFSET ? LIMIT 1");
        Query query = entityManager.createNativeQuery(sql.toString());
        query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
        query.setParameter(1, date);
        query.setParameter(2, days - 1);
        Map continueObj = JSON.parseObject(JSON.toJSONString(query.getSingleResult()), Map.class);
        // 如果顺延的最后一天是休息日, 查找之后的一条工作日
        if(3201 == ((Integer) continueObj.get("iswork"))) {
            sql = new StringBuffer();
            sql.append("select to_char(gregorian, 'yyyy-MM-dd') as gregorian, iswork from ha_calendar where gregorian > ? and iswork = 3200 ORDER BY gregorian OFFSET 0 LIMIT 1");
            query = entityManager.createNativeQuery(sql.toString());
            query.unwrap(NativeQueryImpl.class).setResultTransformer(OffersetTransformers.ALIAS_TO_ENTITY_MAP);
            query.setParameter(1, continueObj.get("gregorian"));
            Map workObj = JSON.parseObject(JSON.toJSONString(query.getSingleResult()), Map.class);
            return (String) workObj.get("gregorian");
        }
        return (String) continueObj.get("gregorian");
    }
}
